package com.tuanzi.loan.business.service.settlement.impl;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.family.ProductInfoStatus;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.ProductLinkInfo;
import com.tuanzi.loan.business.entity.settlement.QualificationInfo;
import com.tuanzi.loan.business.repository.settlement.CompanyInfoRepository;
import com.tuanzi.loan.business.service.family.ProductInfoService;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.business.service.settlement.ProductLinkInfoService;
import com.tuanzi.loan.business.service.settlement.QualificationInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.settlement.CompanyInfoSearchRequest;
import com.tuanzi.loan.vo.response.settlement.CompanyInfoSearchResponse;
import com.tuanzi.loan.vo.view.settlement.CompanyInfoView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-01-12 10:25
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class CompanyInfoServiceImpl extends BaseAuditServiceImpl<CompanyInfo, Long> implements CompanyInfoService {
    @Autowired
    private CompanyInfoRepository companyInfoRepository;

    @Autowired
    private QualificationInfoService qualificationInfoService;

    @Autowired
    private ProductLinkInfoService productLinkInfoService;

    @Autowired
    private ProductInfoService productInfoService;

    @Override
    protected JPAAuditRepository<CompanyInfo, Long> getRepository() {
        return companyInfoRepository;
    }


    @Override
    public Page<CompanyInfoSearchResponse> list(PageSearcher<CompanyInfoSearchRequest> pageSearcher) {
        return companyInfoRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public void saveCompanyInfo(CompanyInfoView companyInfoView, List<QualificationInfo> qualificationInfoList, List<ProductLinkInfo> productLinkInfoList) {
        CompanyInfo companyInfo = null;
        Long id = companyInfoView.getId();
        if (id != null) {
            companyInfo = findOne(id);
        }
        if (companyInfo == null) {
            companyInfo = new CompanyInfo();
        }
        //保存公司信息
        BeanUtils.copyProperties(companyInfoView, companyInfo);
        saveOrUpdate(companyInfo);
        Long companyId = companyInfo.getId();
        companyInfoView.setId(companyId);

        //保存照片信息
        qualificationInfoList.forEach(qualificationInfo -> {
            if (qualificationInfo.getAttachmentUrl() != null){
                qualificationInfo.setCompanyId(companyId);
                qualificationInfoService.saveOrUpdate(qualificationInfo);
            }
            else {
                if (qualificationInfo.getId()!=null) {
                    qualificationInfoService.delete(qualificationInfo.getId());
                }
            }
        });

        //保存产品链接
        productLinkInfoList.forEach(productLinkInfo -> {
            if (productLinkInfo.getProductLink() != null){
                if (productLinkInfo.getId()!=null){
                    ProductLinkInfo one = productLinkInfoService.findOne(productLinkInfo.getId());
                    one.setCompanyId(companyId);
                    one.setProductLink(productLinkInfo.getProductLink());
                    one.setRemark(productLinkInfo.getRemark());
                    productLinkInfoService.saveOrUpdate(one);
                }
                else {
                    productLinkInfo.setCompanyId(companyId);
                    productLinkInfoService.saveOrUpdate(productLinkInfo);
                }

            }
        });


    }

    @Override
    public List<CompanyInfo> findCompaniesByAuditStatus(Integer auditStatus) {
        return companyInfoRepository.findCompaniesByAuditStatus(auditStatus);
    }

    /**
     * 通过公司id改变公司审核的状态
     * @param companyId
     * @param auditStatus
     */
    @Override
    public void changeAuditStatus(String companyId, Integer auditStatus) {
        CompanyInfo companyInfo = findOne(Long.parseLong(companyId));
        if (companyId != null){
            companyInfo.setAuditStatus(auditStatus);
            saveOrUpdate(companyInfo);
            //审核通过保存产品表
            /*if(auditStatus == 4){
                ProductInfo productInfo = new ProductInfo();
                productInfo.setCompanyId(companyInfo.getId());
                productInfo.setName(companyInfo.getProductName());
                productInfo.setStatus(ProductInfoStatus.OFFLINE);
                productInfoService.saveOrUpdate(productInfo);
            }*/
        }
    }

    @Override
    public List<CompanyInfo> findAllCompanyName() {
        return companyInfoRepository.findAllCompanyName();
    }

    @Override
    public CompanyInfo findCompanyInfoByCompanyNameAndProductName(String companyName, String productName) {
        return companyInfoRepository.findCompanyInfoByCompanyNameAndProductName(companyName, productName);
    }

    @Override
    public CompanyInfo findCompanyInfoByCompanyName(String companyName) {
        return companyInfoRepository.findCompanyInfoByCompanyName(companyName);
    }
}
